package com.bwhx.eruler.service.casdilly.impl;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bwhx.eruler.bean.Page;
import com.bwhx.eruler.bean.casdilly.CasdillyCustomer;
import com.bwhx.eruler.bean.casdilly.ClothesType;
import com.bwhx.eruler.bean.casdilly.Contract;
import com.bwhx.eruler.bean.casdilly.ContractStatus;
import com.bwhx.eruler.bean.casdilly.Linkman;
import com.bwhx.eruler.dao.casdilly.CasdillyCustomerDAO;
import com.bwhx.eruler.dao.casdilly.ClothesTypeDAO;
import com.bwhx.eruler.dao.casdilly.ContractDAO;
import com.bwhx.eruler.dao.casdilly.LinkmanDAO;
import com.bwhx.eruler.service.casdilly.LinkmanService;
import com.bwhx.eruler.util.ListUtils;

@Service
public class ContractServiceImpl implements com.bwhx.eruler.service.casdilly.ContractService {

	@Autowired
	ContractDAO contractDAO;

	@Autowired
	ClothesTypeDAO clothesTypeDAO;

	@Autowired
	LinkmanDAO linkmanDAO;

	@Autowired
	CasdillyCustomerDAO ccustomerDAO;

	@Override
	public List<Contract> list(Page entity) {
		return contractDAO.list(entity);
	}

	@Override
	public Integer count(Contract entity) {
		return contractDAO.count(entity);
	}

	@Transactional
	@Override
	public Integer insertProjectAndClothesType(Contract entity) {
		contractDAO.insert(entity);
		String[] split = entity.getcPClothesTypes() == null ? null : entity.getcPClothesTypes().split(",");
		if (split != null && split.length > 0) {
			List<ClothesType> list = clothesTypeDAO.findByIds(Arrays.asList(split));
			for (ClothesType clothesType : list) {
				clothesType.setCreateTime(entity.getCreateTime());
				clothesType.setCreatorID(entity.getCreatorID());
				clothesType.setTypeCategory(1);
				clothesType.setTypeCPIDSerial(entity.getcPIDSerial());

				List<ClothesType> ctList = clothesTypeDAO.findByCTNameAndTypeAndProjectSerial(clothesType);
				if (ctList.size() > 0) {
					return 1;
				}
				clothesTypeDAO.insert(clothesType);
			}
		}
		return 2;

	}

	@Override
	public Integer updateProjectAndClothesType(Contract entity) {
		contractDAO.update(entity);
		ClothesType type = new ClothesType();
		type.setTypeCPIDSerial(entity.getcPIDSerial());
		clothesTypeDAO.deleteByTypeCPIDSerial(type);
		String[] split = entity.getcPClothesTypes() == null ? null : entity.getcPClothesTypes().split(",");
		if (split != null && split.length > 0) {
			List<ClothesType> list = clothesTypeDAO.findByIds(Arrays.asList(split));
			int i=0;
			for (ClothesType clothesType : list) {
				clothesType.setCreateTime(new Date());
				clothesType.setCreatorID(entity.getModifierID());
				clothesType.setTypeCategory(1);
				clothesType.setTypeCPIDSerial(entity.getcPIDSerial());
				clothesType.setDisplayOrder(i);
				List<ClothesType> ctList = clothesTypeDAO.findByCTNameAndTypeAndProjectSerial(clothesType);
				i++;
				if (ctList.size() > 0) {
					return 1;
				}
				clothesTypeDAO.insert(clothesType);
			}
		}
		return 2;
	}

	@Override
	public void delete(Contract entity) {
		contractDAO.delete(entity);

	}

	@Override
	public Contract findBySerial(String getcPIDSerial) {
		return contractDAO.findBySerial(getcPIDSerial);
	}

	@Override
	public List<ContractStatus> listcontractStatus(Page p) {
		return contractDAO.listcontractStatus(p);
	}

	@Override
	public Integer countcontractStatus(Contract contract) {
		return contractDAO.countcontractStatus(contract);
	}

	@Override
	public void insert(Contract entity) {
		contractDAO.insert(entity);
	}

	@Override
	public void update(Contract entity) {
		// TODO Auto-generated method stub

	}

	@Override
	@Transactional
	public void delContractFamily(Contract contract) {

		contractDAO.updateDelFlag(contract);

		List<Linkman> linkManList = linkmanDAO.listByContract(contract);
		if (ListUtils.isSubstantial(linkManList)) {
			for (Linkman linkman : linkManList) {
				linkman.setModifierID(contract.getModifierID());
				linkman.setModifyTime(contract.getModifyTime());
				linkmanDAO.delete(linkman);
			}

			List<CasdillyCustomer> ccustomerList = ccustomerDAO.listByLinkmanList(linkManList);
			if (ListUtils.isSubstantial(ccustomerList)) {
				ccustomerDAO.updateDelFlagByCcustomerList(ccustomerList);
				ccustomerDAO.updateScanDataDelFlagByCcustomerList(ccustomerList);
			}
		}

		List<ClothesType> clothesTypeList = clothesTypeDAO.listByContract(contract);
		if (ListUtils.isSubstantial(clothesTypeList)) {

			for (ClothesType clothesType : clothesTypeList) {
				clothesType.setModifierID(contract.getModifierID());
				clothesType.setModifyTime(contract.getModifyTime());
				clothesTypeDAO.deleteById(clothesType);
			}
		}

	}

}
